package com.example.administrator.myapplication.tools;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.view.Display;

import java.io.File;

/**
 * Created by Administrator on 2018/2/23.
 */

public class PictureFactory {
        public static Bitmap shrinkPic(String filestr, Activity activity){
            BitmapFactory.Options options =new BitmapFactory.Options();
            //只获取高度，不获取像素
            options.inJustDecodeBounds=true;
            Bitmap bitmap =BitmapFactory.decodeFile(filestr,options);
            //获取图片的宽度
            int outWidth =options.outWidth;
            int outHeight =options.outHeight;

            //获取屏幕默认设置
            Display dp =activity.getWindowManager().getDefaultDisplay();
            Point outSize =new Point();
            dp.getSize(outSize);
            //获取屏幕的宽度
            int screenWidth =outSize.x;
            //获取屏幕的高度
            int screenHeight =outSize.y;

            //计算缩放比例 默认为1；
            int scale =1;
            int scaleWidth=outWidth/screenWidth;
            int scaleHeight =outHeight/screenHeight;
            if(scaleWidth>=scaleHeight){
                scale =scaleWidth;//当宽的缩放比例大时，采用宽的缩放比例
            }else{
                scale =scaleHeight;
            }
            //设置缩放比例
            options.inSampleSize=scale;
            //设置获取像素的值
            options.inJustDecodeBounds=false;
            //设置缩小后位图信息
            Bitmap bm=BitmapFactory.decodeFile(filestr,options);
            return bm;

        }
}
